#! python 3
# -*- coding:utf-8 -*-
# Author: sean

import requests
from bs4 import BeautifulSoup
import urllib


class LySpider(object):

    def __init__(self, post_name, url):
        """
        初始化
        :param post_name: 岗位名称
        :param url: 请求地址
        """
        self.post_name = post_name
        self.url = url

    def begin_spider(self):
        """
        开始爬取岗位
        :return:
        """

        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.55",
        }
        x = requests.get(url=self.url, headers=headers)

        if x.status_code == 200:
            html_str = x.text
            soup = BeautifulSoup(html_str, "html.parser")

            # jobName_style = soup.find_all(class_="jobName_style")
            job_name_style = soup.select('a[class="jobName_style"]')  # 岗位名称
            salary_style = soup.select('div[class="salaryStyle"]')    # 薪资
            ent_style = soup.select('div[class="l ent_style1"]')      # 公司
            workplace = soup.select('span[title="工作地点"]')           # 工作地点
            data_len = len(job_name_style)

            if len(job_name_style) > 0:  # 查询到岗位
                for i in range(data_len):

                    post_info = {
                        "岗位": job_name_style[i]["title"],
                        "薪资": salary_style[i].text.replace('\n',""),
                        "公司": ent_style[i].text.replace('\n', ""),
                        "地点": workplace[i].text.replace('\n', "")
                    }
                    print(post_info)
                    #post_list.append(post_info)
                #print(post_list)


if __name__ == '__main__':

    post_name = input("关键字：")
    post_name = urllib.parse.quote(post_name.encode('unicode-escape')).replace('%5Cu', '%u')
    url = f"http://www.lyzp100.com/search/offer_search_result.aspx?keyword={post_name}&jcity1Hidden=291100&sex=undefined&expr=-1&SortWay=0&areatitle=&lat=0&lng=0&zoom=0&ma=0"

    sp = LySpider(post_name, url)
    sp.begin_spider()
